package com.tanhua.server.service;


import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.common.mapper.LogMapper;
import com.tanhua.common.pojo.Log;
import com.tanhua.common.pojo.User;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.api.LogApi;
import com.tanhua.dubbo.server.api.UserLocationApi1;
import com.tanhua.dubbo.server.vo.UserLocationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author why
 * 实现日志存储
 */
@Service
public class LogMQService {

    @Reference(version = "1.0.0")
    private LogApi logApi;

    @Reference(version = "1.0.0")
    private UserLocationApi1 userLocationApi1;

    @Autowired
    private LogMapper logMapper;
    //登录
    public void login(String type){
        Log log=new Log();
        log.setEquipment("移动端");
        User user = UserThreadLocal.get();
        log.setUserId(user.getId());
        log.setType(type);
        log.setLogTime(System.currentTimeMillis());
        UserLocationVo userLocationVo = userLocationApi1.queryByUserId(user.getId());
        log.setPlace(userLocationVo.getAddress());

        this.logMapper.insert(log);
    }


    //发动态
    public void movementsAdd(String type){
           this.sendMsg(type);
    }

    //浏览动态
    public void movementsRead(String type){
           this.sendMsg(type);
    }

    //动态点赞
    public void movementsLike(String type){
           this.sendMsg(type);
    }

    //动态喜欢
    public void movementsLove(String type){
           this.sendMsg(type);
    }

    //动态评论
    public void movementsComment(String type){
           this.sendMsg(type);
    }

    //动态取消点赞
    public void movementsUnlike(String type){
           this.sendMsg(type);
    }

    //动态取消喜欢
    public void movementsUnLove(String type){
           this.sendMsg(type);
    }

    //发小视频
    public void videoAdd(String type){
           this.sendMsg(type);
    }

    //小视频点赞
    public void videoLike(String type){
           this.sendMsg(type);
    }

    //小视频取消点赞
    public void videoUnlike(String type){
           this.sendMsg(type);
    }

    //小视频评论
    public void videoComment(String type){
           this.sendMsg(type);
    }



    //传入参数 调用接口
    private void sendMsg(String type) {
//
//        //该用户进行的操作记录 查看当前的地点
//        User user = UserThreadLocal.get();
////        UserLocationVo userLocationVo = locationApi.queryByUserId(user.getId());
////        String address = userLocationVo.getAddress();
//        try {
//
//            //构建消息
//            Map<String, Object> msg = new HashMap<>();
//
//            //当前用户 long
//            msg.put("userId", user.getId());
//
//            //操作时间  long
//            msg.put("logTime", System.currentTimeMillis());
//
//            //操作类型 string
//            msg.put("type", type);
//
//            //操作地点  string
//            msg.put("place","北京");
//
//            //操作方式  string
//            msg.put("equipment","移动端");
//
//            this.rocketMQTemplate.convertAndSend("tanhua_Log", msg);
//        } catch (Exception e) {
//            LOGGER.error("发送消息失败! userId = " + user.getId() + ", type = " + type, e);
//            return false;
//        }
//
//        return true;

        User user = UserThreadLocal.get();

         this.logApi.save(user.getId(),type);
   }
}
